package us.icitap.cm.ui.policecase;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AssociationType;
import us.icitap.cm.business.entities.EntityEntityAssociation;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.listeners.EntityEntityAssociationListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTable;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustVerticalLayout;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.utils.HandleNotification;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.VerticalLayout;

public class EntityEntityAssocDataLayout extends VerticalLayout{

	private Report report;
	CustTable assocTable = new CustTable("Lidhjet ne ceshtje");
	final BeanItemContainer<EntityEntityAssociation> beans  = new BeanItemContainer<EntityEntityAssociation>(EntityEntityAssociation.class);
	
	
	private final CustComboBox entityTypeOneSelect = new CustComboBox("Lloji i entitetit");
	private final CustComboBox entityOneSelect = new CustComboBox("Entiteti");
	
	private final CustComboBox entityTypeTwoSelect = new CustComboBox("Lloji i entitetit");
	private final CustComboBox entityTwoSelect = new CustComboBox("Entiteti");
	
	private final CustComboBox assocSelect = new CustComboBox("Lidhja");
	
	private final CustTextArea notesTextArea = new CustTextArea("Shenime");
	
	private final BeanItemContainer<AssociationType> associationTypeContainer = new BeanItemContainer<AssociationType>(AssociationType.class);
	private final BeanItemContainer<AppEntity> appEntityOneContainer = new BeanItemContainer<AppEntity>(AppEntity.class);
	private final BeanItemContainer<AppEntity> appEntityTwoContainer = new BeanItemContainer<AppEntity>(AppEntity.class);
	
	private final CustBeanFieldGroup<EntityEntityAssociation> binder = new CustBeanFieldGroup<EntityEntityAssociation>(EntityEntityAssociation.class);
	EntityEntityAssociation entityEntityAssociation;
	
	private EntityEntityAssociationListener listener;
	
	
	 public EntityEntityAssocDataLayout() {
		this.setMargin(true);	
		CustomFormLayout cfl = new CustomFormLayout("600px", "Lidhjet", "Reset", "Ruaj");
		this.addComponent(cfl);
		
		
		
		assocTable.setContainerDataSource(beans);
		assocTable.setVisibleColumns(new Object[]{"entityOne", "associationType", "entityTwo"});
		assocTable.setColumnHeaders(new String[]{"Entiteti", "Lidhja", "Entiteti"});
		assocTable.setPageLength(6);
		assocTable.setImmediate(true);
		
		cfl.addComponent(assocTable);
		cfl.setComponentAlignment(assocTable, Alignment.MIDDLE_CENTER);
		CustVerticalLayout vl = new CustVerticalLayout();
		FormLayout fl = new FormLayout();
		
		fl.addComponent(this.entityTypeOneSelect);
		fl.addComponent(this.entityOneSelect);
		fl.addComponent(this.entityTypeTwoSelect);
		fl.addComponent(this.entityTwoSelect);
		fl.addComponent(this.assocSelect);
		fl.addComponent(notesTextArea);
		vl.addComponent(fl);
		
		cfl.addComponent(vl);
		
		this.assocTable.setWidth("49em");
		this.entityOneSelect.setWidth("40em");
		this.entityTwoSelect.setWidth("40em");
		this.assocSelect.setWidth("30em");
		this.notesTextArea.setWidth("40em");
		cfl.addFooter();
		
		
		this.assocSelect.setContainerDataSource(associationTypeContainer);
		assocSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		assocSelect.setItemCaptionPropertyId("tag");
		assocSelect.setNullSelectionAllowed(false);
		
		entityOneSelect.setNullSelectionAllowed(false);
		entityOneSelect.setContainerDataSource(appEntityOneContainer);
		
		entityTwoSelect.setNullSelectionAllowed(false);
		entityTwoSelect.setContainerDataSource(appEntityTwoContainer);
		
		entityTypeOneSelect.setContainerDataSource(new BeanItemContainer<EntityTypeEnum>(EntityTypeEnum.class, Arrays.asList(EntityTypeEnum.values())));
		entityTypeOneSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		entityTypeOneSelect.setItemCaptionPropertyId("caption");
		entityTypeOneSelect.setNullSelectionAllowed(false);

		entityTypeTwoSelect.setContainerDataSource(new BeanItemContainer<EntityTypeEnum>(EntityTypeEnum.class, Arrays.asList(EntityTypeEnum.values())));
		entityTypeTwoSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		entityTypeTwoSelect.setItemCaptionPropertyId("caption");
		entityTypeTwoSelect.setNullSelectionAllowed(false);
		
		entityTypeOneSelect.setImmediate(true);
		entityTypeOneSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() != null){
					if(event.getProperty().getValue() != null){
						EntityTypeEnum ete = (EntityTypeEnum) event.getProperty().getValue();
						List<AppEntity> list = report.getAppEntityByType(ete);
						appEntityOneContainer.removeAllItems();
						if(list != null)  appEntityOneContainer.addAll(list);
						
						
						if(entityTypeTwoSelect.getValue() != null){
							EntityTypeEnum eteTwo = (EntityTypeEnum) entityTypeTwoSelect.getValue();
							Result<List<AssociationType>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAssociationType(ete.name() + "_" + eteTwo.name());
							if(result.isSucceeded()){
								associationTypeContainer.removeAllItems();
								associationTypeContainer.addAll(result.getObject());
							}
							else HandleNotification.showWarningOnScreen(result.getMessage());
						}
						
					}
					else {
						associationTypeContainer.removeAllItems();
					}
				}
				
			}
		});
		
		entityTypeTwoSelect.setImmediate(true);
		entityTypeTwoSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty() != null){
					if(event.getProperty().getValue() != null){
						EntityTypeEnum ete = (EntityTypeEnum) event.getProperty().getValue();
						List<AppEntity> list = report.getAppEntityByType(ete);
						appEntityTwoContainer.removeAllItems();
						if(list != null) appEntityTwoContainer.addAll(list);
						if(entityTypeOneSelect.getValue() != null){
							EntityTypeEnum eteOne = (EntityTypeEnum) entityTypeOneSelect.getValue();
							System.out.println(eteOne.name() + "-" + ete.name());
							Result<List<AssociationType>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAssociationType(eteOne.name() + "_" + ete.name());
							if(result.isSucceeded()){
								System.out.println("result");
								associationTypeContainer.removeAllItems();
								associationTypeContainer.addAll(result.getObject());
							}
							else HandleNotification.showWarningOnScreen(result.getMessage());
						}
						
					}
					else {
						associationTypeContainer.removeAllItems();
					}
					

				}
				
			}
		});

		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					
					entityEntityAssociation.setRecordDate(new Date());
					
//					report.addEntityEntityAssociation(entityEntityAssociation);
					beans.addBean(entityEntityAssociation);
					if(listener != null) listener.addEntityEntityAssociation(entityEntityAssociation);
					
					loadNewAssociation();
					
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		

		binder.bind(this.entityOneSelect, "entityOne");
		binder.bind(this.entityTwoSelect, "entityTwo");
		binder.bind(this.assocSelect, "associationType");
		binder.bind(this.notesTextArea, "notes");
		
		
	}
	
	 
	 public void loadNewAssociation(){
		 entityEntityAssociation = new EntityEntityAssociation();
		
		 binder.setItemDataSource(entityEntityAssociation);
		 this.entityTypeOneSelect.setValue(null);
		 this.entityTypeTwoSelect.setValue(null);
		 this.entityOneSelect.setValue(null);
		 this.entityTwoSelect.setValue(null);
		 this.assocSelect.setValue(null);
	 }
	 
	 
	 public void setReport(Report report){
		 this.report = report;
		 this.loadNewAssociation();
		 beans.removeAllItems();
		 if (report.getEntityEntityAssocList() != null) beans.addAll(report.getEntityEntityAssocList());
	 }

	
	public void addEntityEntityAssociationListener(EntityEntityAssociationListener eeaListener){
		this.listener = eeaListener;
	}

}
